<?php

/**
 * @Author: Mr.Mao
 * @Date:   2021-05-31 09:46:50
 * @Last Modified by:   Mr.Mao
 * @Last Modified time: 2021-06-03 10:31:47
 */
/**
 * 腾讯云存储
 */
require('sdk/autoload.php');
use app\mainadmin\model\StorageModel;
use Qiniu\Auth;
use Qiniu\Config;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
// use Qiniu\Storage\ArgusManager;
// use Qiniu\Storage\FormUploader;
// use Qiniu\Storage\ResumeUploader;
class storage
{
	
	public $config;
	public $obj;
	public $bucket;
	public $region;
	// 初始化
	public function __construct(){
		$where = [];
		$where[] = ['type','=','qiniu'];
		$info = (new StorageModel)->getInfo($where);
		$this->config = $info['config'];
		$this->obj = new Auth($this->config['accessKey'], $this->config['secretKey']);
		$this->bucket = $this->config['bucket'];
		$this->region = $this->config['region'];
	}
	/**
	 * [createBucket 创建空间]
	 * @param  string $bucket [空间名称]
	 */
	public function createBucket($bucket='')
	{
		$config = new Config();
		$bucketManager = new BucketManager($this->obj, $config);
		list($ret, $err) = $bucketManager->createBucket($bucket,$this->region);
		if ($err != null) {
			$data['code'] = 0;
			$data['msg'] = $err->message();
		    //请求失败
		    return $data;
		} 
		$data['code'] = 1;
	    return $data;
	}
	
	/**
	 * [getBucketList 获取空间列表]
	 */
	public function getBucketList()
	{
		$config = new Config();
		$bucketManager = new BucketManager($this->obj, $config);
		list($ret, $err) = $bucketManager->listbuckets();
		if ($err != null) {
			$data['code'] = 0;
			$data['msg'] = $err->message();
		    //请求失败
		    return $data;
		} 
		$data['code'] = 1;
	    $data['list'] = $ret;
	    return $data;
	}

	/**
	 * [deleteBucket 删除空间]
	 * @param  string $bucket [空间名称]
	 */
	public function deleteBucket($bucket='')
	{
		$config = new Config();
		$bucketManager = new BucketManager($this->obj, $config);
		list($ret, $err) = $bucketManager->deleteBucket($bucket);
		if ($err != null) {
			$data['code'] = 0;
			$data['msg'] = $err->message();
		    //请求失败
		    return $data;
		} 
		$data['code'] = 1;
	    return $data;
	}

	/**
	 * [deleteBucket 列取空间的文件列表]
	 * @param  string $bucket [空间名称]
	 */
	public function listObjects()
	{
		$config = new Config();
		$bucketManager = new BucketManager($this->obj, $config);
		list($ret, $err) = $bucketManager->listFiles($this->bucket);
		if ($err != null) {
			$data['code'] = 0;
			$data['msg'] = $err->message();
		    //请求失败
		    return $data;
		}
		$data['code'] = 1;
		$data['list'] = $ret['items'];
	    return $data;
	}

	/**
	 * [upload 上传文件]
	 * @param  string $object   [文件名称]
	 * @param  array  $file   	[上传的文件]
	 */
	public function upload($object = '',$file = [])
	{
		$policy = [];
		$uptoken = $this->obj->uploadToken($this->bucket, $object, 3600, $policy);
		$uploadMgr = new UploadManager();
		list($ret, $err) = $uploadMgr->putFile($uptoken, $object, $file['tmp_name']);
		if ($err != null) {
			$data['code'] = 0;
			$data['msg'] = $err->message();
		    //请求失败
		    return $data;
		}
		$data['code'] = 1;
		$data['url'] = $this->config['domain'] . $ret['key'];
	    return $data;
	}
	/**
     * [upload 对资源进行复制。]
     * @param string $from_key 待操作资源文件名
     * @param string $to_bucket 目标资源空间名
     * @param string $to_key 目标资源文件名
     */
	public function copyObject($from_key,$to_key,$to_bucket ='')
	{
		if (empty($to_bucket)) {
			$to_bucket = $this->bucket;
		}
		$config = new Config();
		$bucketManager = new BucketManager($this->obj, $config);
		list($ret, $err) = $bucketManager->copy($this->bucket,$from_key,$to_bucket,$to_key);
		if ($err != null) {
			$data['code'] = 0;
			$data['msg'] = $err->message();
		    //请求失败
		    return $data;
		}
		$data['code'] = 1;
	    return $data;
	}

	/**
     * [upload 删除指定资源]
     *
     * @param string $bucket 待删除资源所在的空间
     * @param string $object 待删除资源的文件名
     */
    public function deleteObject($object='')
    {
        $config = new Config();
		$bucketManager = new BucketManager($this->obj, $config);
		list($ret, $err) = $bucketManager->delete($this->bucket,$object);
		if ($err != null) {
			$data['code'] = 0;
			$data['msg'] = $err->message();
		    //请求失败
		    return $data;
		}
		$data['code'] = 1;
	    return $data;
    }
    /**
     * [upload 删除指定多个资源]
     *
     * @param string $bucket 待删除资源所在的空间
     * @param array  $objects [删除对象]
     */
    public function deleteObjects($objects = [])
    {
        $config = new Config();
		$bucketManager = new BucketManager($this->obj, $config);
		foreach ($objects as $object) {
			$bucketManager->delete($this->bucket,$object);
		}
		
		$data['code'] = 1;
	    return $data;
    }
}